Traducción al español será siempre la mayor brevedad

inputFile


The inputFile component renders an file input HTML element. Users specify a file to upload either by entering the path to a file directly, or by clicking the Browse button to open a file-system navigation dialog window. Clicking the Upload button uploads the specified file to the server.

The inputFile component can be used to provide a user-specified file upload capability.

The inputFile component has two listeners progressListener and actionListener. The progressListener can notify the application on the percentage of the file being uploaded while the actionListener notifies the application about the state of the file. The progressListener is optional and only required when the Developer would like to inform the user of the File upload progress.

Note:  - The inputFile component must be inside a form element.
- The inputFile component sends exception information to the client using FacesMessage, so it always a good idea to have a instance of message or messages for inputFile component and if the developer wants to perform some operation against an exception he/she could do it using actionListener.

progressListener: These are some things to consider when implementing the progressListener.

  1. Method must take a single argument of type java.util.EventObject.
  2. Developer must invoke the render() call on the PersistentFacesState instance.
  3. Beans which implement the progressListener must be in session scope.
  4. The PersistentFacesState must be set in the constructor of session bean.
The following example shows an InputFile progressListener implementation in a bean.
public class MyBean {
    private PersistentFacesState state = null;
        private int percent = -1;
        public MyBean() {
            state = PersistentFacesState.getInstance();
        }

       /**
        * This is the progressListener implementation
        */
        public void progress (EventObject event) {
             InputFile file = (InputFile)event.getSource();
             percent  = file.getFileInfo().getPercent();
             try {
                  if (state != null) {
                     state.render();
                 }
             } catch (RenderingException e) {
                 e.printStackTrace();
             }
     }
}

actionListener:
These are some things to consider when implementing the actionListener.

  1. The actionListener is only invoked when the file is saved or on any exception.
  2. The developer can make a decision using the following 4 InputFile states.
    [SAVED, INVALID, SIZE_LIMIT_EXCEEDED, UNKNOWN_SIZE].
The following example shows an InputFile actionListener implementation.
public void action(ActionEvent event){
      InputFile inputFile =(InputFile) event.getSource();
      //file has been saved
       if (inputFile.getStatus() == InputFile.SAVED) {
            fileName = inputFile.getFileInfo().getFileName();
            contentType = inputFile.getFileInfo().getContentType();
      }

       //invalid file, happens when clicking on upload without
 selecting a file, or a file with no contents.
       if (inputFile.getStatus() == InputFile.INVALID) {
            inputFile.getFileInfo().getException().printStackTrace();
       }

     //file size exceeded the limit
      if (inputFile.getStatus() == InputFile.SIZE_LIMIT_EXCEEDED) {
          inputFile.getFileInfo().getException().printStackTrace();
      }

     //indicate that the request size is not specified.
      if (inputFile.getStatus() == InputFile.UNKNOWN_SIZE) {
           inputFile.getFileInfo().getException().printStackTrace();
     }
  }

Example

The following code shows how to create a simple inputFile component:

<ice:inputFile />
Tag Summary

tag-name:
<ice:inputFile>
tag-class:
com.icesoft.faces.component.inputfile.InputFileTag
component-class:
com.icesoft.faces.component.inputfile.InputFile
component-type:
com.icesoft.faces.File
component-family:
com.icesoft.faces.File
renderer-class:
com.icesoft.faces.component.inputfile.InputFileRenderer
renderer-type:
com.icesoft.faces.Upload